from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support import expected_conditions as ec
from comm.tools import *


class CommonDriver:
    @classmethod
    def get_driver(self):
        """
        获取WebDriver实例并进行初始化。

        :return: WebDriver实例
        """
        # 从配置中提取所需的信息
        desc = {
            'platformName': 'Android',  # 目标平台，Android系统
            'platformVersion': '7',  # Android版本，指定为7.x
            'appPackage': 'com.dangdang.buy2',  # 要启动的应用包名
            'appActivity': 'com.dangdang.buy2.StartupActivity',  # 应用的启动Activity
            'noReset': False,  # 设置为False以便在每次测试时重置应用的状态
            'unicodeKeyboard': True,  # 使用Unicode键盘输入，适用于输入中文等特殊字符
            'udid': '127.0.0.1:62001',  # 目标设备的唯一标识符（UDID），这里是连接的Android模拟器或设备
        }

        # 定义命令执行器的URL，指向运行中的Appium Server
        command_executor_url = 'http://127.0.0.1:4723/wd/hub'

        # 创建WebDriver实例，连接到Appium Server，并传入所需的能力配置
        driver = webdriver.Remote(command_executor=command_executor_url, desired_capabilities=desc)

        # 设置隐式等待时间为10秒，以便在查找元素时等待最大10秒
        driver.implicitly_wait(10)

        # 输出连接成功的信息
        print("连接成功")

        # 返回已初始化的WebDriver实例
        return driver

    @classmethod
    def fing_element_new(self, driver, by, value, time_out=10):
        try:
            el1 = WebDriverWait(driver, time_out).until(ec.visibility_of_element_located(
                    (by, value)
                )
            )
            return el1

        except BaseException as b:
            print(b)
        finally:
            print(f'元素未找到{by}--{value}')

    @classmethod
    def click_new(self, driver, by, value):
        try:
            el2 = self.fing_element_new(driver, by, value)
            el2.click()
            print(f'已经点击元素\t{by}--{value}')
        except Exception as e:
            print(e)
            print(f'无法点击元素\t{by}--{value}')

    @classmethod
    def send_keys_new(self, driver, by, value, text):
        try:
            el3 = self.fing_element_new(driver, by, value)
            el3.clear()
            el3.send_keys(text)
            print(f'成功输入内容\t{by}--{value}--{text}')
        except Exception as e:
            print(e)
            print(f'无法输入内容\t{by}--{value}--{text}')


if __name__ == '__main__':
    driver = CommonDriver.get_driver()
    CommonDriver.click_new(driver, AppiumBy.XPATH,
                           '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]')
    CommonDriver.click_new(driver, AppiumBy.XPATH,
                           '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]')
    CommonDriver.click_new(driver, AppiumBy.XPATH,
                           '//android.widget.Button[@resource-id="com.android.packageinstaller:id/permission_allow_button"]')
    CommonDriver.click_new(driver, AppiumBy.XPATH,
                           '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]')
    CommonDriver.click_new(driver, AppiumBy.XPATH,
                           '//android.widget.Button[@resource-id="com.android.packageinstaller:id/permission_allow_button"]')
    CommonDriver.click_new(driver, AppiumBy.XPATH,
                           '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]')

    CommonDriver.send_keys_new(driver, AppiumBy.XPATH,
                               '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]', '魏总牛逼')

